#ifndef __WIN32__
#include <arpa/inet.h>
#else
#include <ws2tcpip.h>
#endif
#include "vcd-node.h"
#include "../../vcd-message-dialog.h"
#include "../../../vcd-common-api/vcd-conf/vcd-conf.h"
#include "../../../vcd-common-api/vcd-common.h"

GtkWidget *g_combo_yun;
GtkWidget *g_btn_server_ip_ok;
GtkWidget *g_mode_pve;
GtkWidget *g_entry_node;

static void change_loginway(void);
static void server_ip_save(void);
static void init_display(void);
static void change_loginmode(void);

static void server_ip_save(void)
{
//#ifndef __WIN32__
    snprintf(g_vcd_conf->ip,sizeof(g_vcd_conf->ip),"%s", gtk_entry_get_text(GTK_ENTRY(g_entry_node)));
    //int ret = inet_pton(AF_INET, tmp, buf);
    //if (ret == 1) {
    change_heartbeat_host(g_vcd_conf->ip);
        update_vcd_conf();
        message_box("  操作成功！  ");
    //} else {//invalid ip
    //    message_box("  操作失败！  ");
    //    M_DEBUG("%s is invalid ip\n", tmp);
    //}
//#endif
}

static void change_loginway(void)
{
    gint login_way = PRIVATE_LOGIN;
    const char *tmp_login_way = gtk_combo_box_text_get_active_text((GtkComboBoxText *)(g_combo_yun));
    if (strcmp(tmp_login_way, "公有云") == 0) {
        gtk_widget_set_sensitive(g_mode_pve, FALSE);
        gtk_widget_set_sensitive(g_btn_server_ip_ok, FALSE);
        gtk_widget_set_can_focus(g_entry_node, FALSE);
        set_css(g_entry_node, "entry{ background:rgb(250,250,250);"
                "border:1px solid rgb(204,204,204);"
                "border-radius:0px;"
                "padding-left:12px}");
        login_way = PUBLIC_LOGIN;
    } else {
        gtk_widget_set_sensitive(g_mode_pve, TRUE);
        gtk_widget_set_sensitive(g_btn_server_ip_ok, TRUE);
        gtk_widget_set_can_focus(g_entry_node, TRUE);
        set_css(g_entry_node, "entry{ background:rgb(255,255,255);"
                "border:1px solid rgb(204,204,204);"
                "border-radius:0px;"
                "padding-left:12px}");
        login_way = PRIVATE_LOGIN;
    }

    g_vcd_conf->login_way = login_way;
    update_vcd_conf();
}

static void change_loginmode(void)
{
    gboolean bres = gtk_toggle_button_get_active((GtkToggleButton *)g_mode_pve);
    g_vcd_conf->login_mode = (gint)bres;
    update_vcd_conf();
}

static void init_display(void)
{
    struct vcd_conf* cf = g_vcd_conf;

    gboolean bres = (cf->login_mode == EMODE_OLD ? TRUE : FALSE);
    gtk_toggle_button_set_active((GtkToggleButton *)g_mode_pve, bres);

    if (PUBLIC_LOGIN == cf->login_way) {
        // gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT((GtkComboBoxText *)g_combo_yun), NULL, "公有云");
        // gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT((GtkComboBoxText *)g_combo_yun), NULL, "私有云");
        // gtk_widget_set_sensitive(g_btn_server_ip_ok, FALSE);
        // gtk_widget_set_sensitive(g_mode_pve, FALSE);
        // gtk_widget_set_can_focus(g_entry_node, FALSE);
        // set_css(g_entry_node, "entry{ background:rgb(250,250,250);"
        //         "border:1px solid rgb(204,204,204);"
        //         "border-radius:0px;"
        //         "padding-left:12px}");
    } else {
        gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT((GtkComboBoxText *)g_combo_yun), NULL, "私有云");
        //gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT((GtkComboBoxText *)g_combo_yun), NULL, "公有云");
        gtk_widget_set_sensitive(g_btn_server_ip_ok, TRUE);
        gtk_widget_set_sensitive(g_mode_pve, TRUE);
        gtk_widget_set_can_focus(g_entry_node, TRUE);
        set_css(g_entry_node, "entry{ background:rgb(255,255,255);"
                "border:1px solid rgb(204,204,204);"
                "border-radius:0px;"
                "padding-left:12px}");
    }
    gtk_combo_box_set_active(GTK_COMBO_BOX((GtkComboBox *)g_combo_yun), 0);
}

GtkWidget *get_sys_loginway_fix(void)
{
    GtkWidget *fix_sys_loginway = gtk_fixed_new();
    GtkWidget *tab4_label7, *tab4_label8;
    struct vcd_conf* cf = g_vcd_conf;

    tab4_label7 = gtk_label_new("登录方式");
    tab4_label8 = gtk_label_new(" 服务地址:");
    g_mode_pve = gtk_check_button_new_with_label("使用原模式");

    g_combo_yun = gtk_combo_box_text_new();
    gtk_widget_set_size_request(g_combo_yun, 173, 20);

    g_btn_server_ip_ok = gtk_button_new_with_label("确定");
    gtk_widget_set_size_request(g_btn_server_ip_ok, 100, 20);
    gtk_button_set_relief(GTK_BUTTON(g_btn_server_ip_ok), GTK_RELIEF_NONE);

    g_entry_node = gtk_entry_new();
    gtk_entry_set_text(GTK_ENTRY(g_entry_node), cf->ip);//设置上一次连接的节点

    init_display();

    //gtk_fixed_put((GtkFixed *)fix_sys_loginway, tab4_label7,  100, 60);
    gtk_fixed_put((GtkFixed *)fix_sys_loginway, tab4_label8,  100, 106);
    //gtk_fixed_put((GtkFixed *)fix_sys_loginway, g_combo_yun,  200, 52);
    gtk_fixed_put((GtkFixed *)fix_sys_loginway, g_entry_node, 200, 98);
    gtk_fixed_put((GtkFixed *)fix_sys_loginway, g_btn_server_ip_ok, 400, 320);
    //gtk_fixed_put((GtkFixed *)fix_sys_loginway, g_mode_pve,   392, 105);

    g_signal_connect(G_OBJECT(g_combo_yun),        "changed", G_CALLBACK(change_loginway),    NULL);
    g_signal_connect(G_OBJECT(g_btn_server_ip_ok), "clicked", G_CALLBACK(server_ip_save),     NULL);
    //g_signal_connect(G_OBJECT(g_mode_pve),         "toggled", G_CALLBACK(change_loginmode), NULL);

    set_css(g_btn_server_ip_ok,  "button{ color: white;"
            "background:rgb(53,126,169);"
            "border:1px solid rgb(40,94,142);"
            "border-radius: 0px}");
    set_css(g_btn_server_ip_ok,  "button:hover{border-radius:0px;background: #3070a9}");

    return fix_sys_loginway;
}
